package j2024.j202408;

import java.util.*;

public class j0803 {
    public static String compressString (String param) {
        // write code here
        Stack<Character> stack  = new Stack<>();
        char[] s = param.toCharArray();
        stack.push(s[0]);
        int i = 1;
        while(i < s.length){
            if(stack.peek()==s[i]){
                int count = 1;
                while(i<s.length && stack.peek()==s[i]){
                    count++;
                    i++;
                }
                    int[] arr  = new int[5];
                int j = 0;
                while (count>0){
                    arr[j++] = count%10;
                    count/=10;
                }
                for (int k = j-1; k >=0 ; k--) {
                    stack.push((char)(arr[k]+'0'));
                }
            }else {
                stack.push(s[i]);
                i++;
            }
        }
        StringBuilder sb = new StringBuilder();
        while (!stack.isEmpty()){
            sb.append(stack.pop());
        }
       return sb.reverse().toString();

    }

    public static void main1(String[] args) {
        String a = "aabcccccaaa";
        String s = compressString(a);
        System.out.println(s);
    }

    public static void main2(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int k = in.nextInt();
        long[] ai = new long[n];
        long[] bi =new long[n];
        boolean[] ni = new boolean[n];
        for (int i = 0; i < n; i++) {
            ai[i] = in.nextLong();
        }
        for (int i = 0; i < n; i++) {
            bi[i] = in.nextLong();
        }
        long sumSweet = 0;
        long sumSoft = 0;
        while(k>1){
            int left  = 0,li = 0;
            int right = n-1,ri = 0;
            long leftSweet = 0,leftSoft = 0;
            long rightSweet = 0,rightSoft = 0;
            while(left<right){
                if(bi[left]>leftSweet && !ni[left]){
                    leftSweet = bi[left];
                    leftSoft  = ai[left];
                    li = left;
                }else if(bi[left]==leftSweet&& !ni[left]){
                    if(ai[left]<leftSoft){
                        leftSoft = ai[left];
                        li = left;
                    }
                }
                if(bi[right]>rightSweet&& !ni[right]){
                    rightSweet = bi[right];
                    rightSoft = ai[right];
                    ri = right;
                }else if(bi[right]==rightSweet&& !ni[right]){
                    if(ai[right]<rightSoft){
                        rightSoft = ai[right];
                        ri = right;
                    }
                }
                left++;
                right--;
            }
            ni[li] = true;
            ni[ri] = true;
            sumSweet+=(rightSweet+leftSweet);
            sumSoft+=(rightSoft+leftSoft);
            k-=2;
        }
        if(k==1){
            long maxa = 0;
            long maxb = 0;
            for (int i = 0; i < n; i++) {
                if(bi[i]>maxb && !ni[i]){
                    maxb = bi[i];
                    maxa = ai[i];
                }else if(bi[i]==maxb && !ni[i]){
                    if(ai[i]<maxb){
                        maxa = ai[i];
                    }
                }
            }
            sumSweet+=maxb;
            sumSoft+=maxa;
        }
        System.out.println(sumSoft+" "+sumSweet);
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int k = in.nextInt();
        long[] ai = new long[n];
        long[] bi =new long[n];
        for (int i = 0; i < n; i++) {
            ai[i] = in.nextLong();
        }
        for (int i = 0; i < n; i++) {
            bi[i] = in.nextLong();
        }
        int left  = 0;
        int right = n-1;
        long leftSweet = bi[0],leftSoft = ai[0];
        long rightSweet = 0,rightSoft = 0;
        while(left<right){
            if(bi[left]>leftSweet){
                leftSweet = bi[left];
                leftSoft  = ai[left];
            }else if(bi[left]==leftSweet){
                if(ai[left]<leftSoft){
                    leftSoft = ai[left];
                }
            }
            if(bi[right]>rightSweet){
                rightSweet = bi[right];
                rightSoft = ai[right];
            }else if(bi[right]==rightSweet){
                if(ai[right]<rightSoft){
                    rightSoft = ai[right];
                }
            }
            left++;
            right--;
        }
        System.out.println((leftSoft+rightSoft)+" "+(rightSweet+leftSweet));
    }

    /**
     * 526. 优美的排列
     */
    int count = 0;
    boolean[]  check;
    public int countArrangement(int n) {
        check = new boolean[n+1];

        dfs(1,n);
        return count;
    }
    public void dfs(int pos,int n){
        if(pos==n+1){
            count++;
            return;
        }
        for (int i = 1; i <= n; i++) {
            if(check[i]==false && (i%pos==0||pos%i==0)){
                check[i] = true;
                dfs(pos+1,n);
                check[i] = false;
            }
        }
    }

    /**
     * 904. 水果成篮
     * @param fruits
     * @return
     */
    public int totalFruit(int[] fruits) {
        HashMap<Integer,Integer> map = new HashMap<>();
        int n = fruits.length;
        int left = 0;
        int right = 0;
        int maxTree = 0;
        while(right<n){
            map.put(fruits[right],map.getOrDefault(fruits[right],0)+1);
            while(map.size()>2){
                map.put(fruits[left],map.get(fruits[left])-1);
                if(map.get(fruits[left])==0){
                    map.remove(fruits[left]);
                }
                left++;
            }
            maxTree = Math.max(maxTree,right-left+1);
            right++;
        }
        return maxTree;
    }

    /**
     * 438. 找到字符串中所有字母异位词
     * @param ss
     * @param pp
     * @return
     */
    public List<Integer> findAnagrams(String ss, String pp) {
        List<Integer> ret = new ArrayList<>();
        char[] s = ss.toCharArray();
        char[] p = pp.toCharArray();
        int[] hs = new int[26];
        int[] hp = new int[26];
        for(char ch: p){
            hp[ch-'a']++;
        }
        int count = 0;
        for (int left = 0,right = 0; right < s.length; right++) {
            char in = s[right];
            hs[in-'a']++;
            if(hs[in-'a']<=hp[in-'a']){
                count++;
            }
            if(right-left+1>p.length){
                char out = s[left];
                if(hs[out-'a']<=hp[out-'a']){
                    count--;
                }
                left++;
                hs[out-'a']--;
            }
            if(count==p.length){
                ret.add(left);
            }
        }
        return ret;
    }
}
